package com.botao.tip.config;

import com.botao.tip.utils.TokenUtil;
import io.jsonwebtoken.Claims;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("PUT", "DELETE", "GET", "POST", "OPTION")
                .allowedHeaders("*")
                .exposedHeaders("access-control-allow-headers",
                        "access-control-allow-methods",
                        "access-control-allow-origin",
                        "access-control-max-age",
                        "X-Frame-Options")
                .allowCredentials(false).maxAge(3600);

    }

    /**
     * 拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new HandlerInterceptor() {
            @Override
            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
                if (HttpMethod.OPTIONS.toString().equals(request.getMethod())) {
                    System.out.println("OPTIONS请求，放行");
                    response.setStatus(HttpServletResponse.SC_OK);
                    return true;
                }
                //获取token
                String token = request.getHeader("token");
                if (TokenUtil.verify(token)) {
                    Claims tokenClaims = TokenUtil.getTokenClaims(token);
                    //获取用户名
                    assert tokenClaims != null;
                    String subject = tokenClaims.getSubject();
                    Integer role = (Integer) tokenClaims.get("role");
                    String url = request.getRequestURI();
                    System.out.println("请求路径：" + url);
                    switch (role) {
                        case 0:
                            System.out.println("用户");
                            break;
                        case 1:
                            System.out.println("管理员");
                            break;
                        case 2:
                            System.out.println("超级管理员");
                            break;
                        default:
                            System.out.println("未知");
                    }
                    return true;
                } else {
                    return false;
                }
            }
        }).addPathPatterns("/**")
                .excludePathPatterns(
                        "/api/login",
                        "/api/website/info",
                        "/api/err"
                );
    }
}
